home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BNU22SR1.ZIP / src / binutils.2 / bfd / coff-z8k.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  7KB  |  257 lines

  1. /* BFD back-end for Zilog Z800n COFF binaries.
  2.    Copyright 1992, 1993 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.    Written by Steve Chamberlain, <sac@cygnus.com>.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "libbfd.h"
  25. #include "obstack.h"
  26. #include "coff/z8k.h"
  27. #include "coff/internal.h"
  28. #include "libcoff.h"
  29. #include "seclet.h"
  30.  
  31. extern bfd_error_vector_type bfd_error_vector;
  32.  
  33.  
  34.  
  35. static reloc_howto_type r_imm32 =
  36. HOWTO (R_IMM32, 0, 1, 32, false, 0, true,
  37.        true, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
  38.  
  39. static reloc_howto_type r_imm4l =
  40. HOWTO (R_IMM4L, 0, 1, 4, false, 0, true,
  41.        true, 0, "r_imm4l", true, 0xf, 0xf, false);
  42.  
  43. static reloc_howto_type r_da =
  44. HOWTO (R_IMM16, 0, 1, 16, false, 0, true,
  45.        true, 0, "r_da", true, 0x0000ffff, 0x0000ffff, false);
  46.  
  47. static reloc_howto_type r_imm8 =
  48. HOWTO (R_IMM8, 0, 1, 8, false, 0, true,
  49.        true,0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
  50.  
  51. static reloc_howto_type r_jr =
  52. HOWTO (R_JR, 0, 1, 8, true, 0, true, true, 0, "r_jr", true, 0, 0, true);
  53.  
  54. /* Turn a howto into a reloc number */
  55.  
  56. static int 
  57. coff_z8k_select_reloc (howto)
  58.      reloc_howto_type *howto;
  59. {
  60.   return howto->type;
  61. }
  62.  
  63. #define SELECT_RELOC(x,howto) x= coff_z8k_select_reloc(howto)
  64.  
  65.  
  66. #define BADMAG(x) Z8KBADMAG(x)
  67. #define Z8K 1            /* Customize coffcode.h */
  68. #define __A_MAGIC_SET__
  69.  
  70.  
  71.  
  72. /* Code to swap in the reloc */
  73. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  74. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  75. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  76.   dst->r_stuff[0] = 'S'; \
  77.   dst->r_stuff[1] = 'C';
  78.  
  79. /* Code to turn a r_type into a howto ptr, uses the above howto table
  80.    */
  81.  
  82. static void
  83. DEFUN (rtype2howto, (internal, dst),
  84.        arelent * internal AND
  85.        struct internal_reloc *dst)
  86. {
  87.   switch (dst->r_type)
  88.     {
  89.       default:
  90.       printf ("BAD 0x%x\n", dst->r_type);
  91.     case R_IMM8:
  92.       internal->howto = &r_imm8;
  93.       break;
  94.      case R_IMM16:
  95.       internal->howto = &r_da;
  96.       break;
  97.     case R_JR:
  98.       internal->howto = &r_jr;
  99.       break;
  100.     case R_IMM32:
  101.       internal->howto = &r_imm32;
  102.       break;
  103.     case R_IMM4L:
  104.       internal->howto = &r_imm4l;
  105.       break;
  106.     }
  107. }
  108.  
  109. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  110.  
  111.  
  112. /* Perform any necessaru magic to the addend in a reloc entry */
  113.  
  114.  
  115. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  116.  cache_ptr->addend =  ext_reloc.r_offset;
  117.  
  118.  
  119. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  120.  reloc_processing(relent, reloc, symbols, abfd, section)
  121.  
  122. static void 
  123. DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
  124.        arelent * relent AND
  125.        struct internal_reloc *reloc AND
  126.        asymbol ** symbols AND
  127.        bfd * abfd AND
  128.        asection * section)
  129. {
  130.   relent->address = reloc->r_vaddr;
  131.   rtype2howto (relent, reloc);
  132.  
  133.   if (reloc->r_symndx > 0)
  134.     {
  135.       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  136.     }
  137.   else
  138.     {
  139.       relent->sym_ptr_ptr = &(bfd_abs_symbol);
  140.     }
  141.  
  142.  
  143.   relent->addend = reloc->r_offset;
  144.   relent->address -= section->vma;
  145. }
  146.  
  147. static void
  148. extra_case (in_abfd, seclet, reloc, data, src_ptr, dst_ptr)
  149.      bfd *in_abfd;
  150.      bfd_seclet_type *seclet;
  151.      arelent *reloc;
  152.      bfd_byte *data;
  153.      unsigned int *src_ptr;
  154.      unsigned int *dst_ptr;
  155. {
  156.   switch (reloc->howto->type)
  157.     {
  158.     case R_IMM8:
  159.       bfd_put_8 (in_abfd, bfd_coff_reloc16_get_value (reloc, seclet),
  160.          data + *dst_ptr);
  161.       (*dst_ptr) += 1;
  162.       (*src_ptr) += 1;
  163.       break;
  164.  
  165.     case R_IMM32:
  166.       bfd_put_32 (in_abfd, bfd_coff_reloc16_get_value (reloc, seclet),
  167.           data + *dst_ptr);
  168.       (*dst_ptr) += 4;
  169.       (*src_ptr) += 4;
  170.       break;
  171.  
  172.     case R_IMM4L:
  173.       bfd_put_8 (in_abfd,
  174.          (bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0) 
  175.          | (0x0f & bfd_coff_reloc16_get_value (reloc, seclet)),
  176.          data + *dst_ptr);
  177.       (*dst_ptr) += 1;
  178.       (*src_ptr) += 1;
  179.       break;
  180.  
  181.     case R_IMM16:
  182.       bfd_put_16 (in_abfd, bfd_coff_reloc16_get_value (reloc, seclet),
  183.           data + *dst_ptr);
  184.       (*dst_ptr) += 2;
  185.       (*src_ptr) += 2;
  186.       break;
  187.  
  188.     case R_JR:
  189.       {
  190.     bfd_vma dst = bfd_coff_reloc16_get_value (reloc, seclet);
  191.     bfd_vma dot = seclet->offset
  192.     + *dst_ptr
  193.     + seclet->u.indirect.section->output_section->vma;
  194.     int gap = dst - dot - 1;/* -1 since were in the odd byte of the
  195.                     word and the pc's been incremented */
  196.  
  197.     if (gap & 1)
  198.       abort ();
  199.     gap /= 2;
  200.     if (gap > 128 || gap < -128)
  201.       {
  202.         bfd_error_vector.reloc_value_truncated (reloc, seclet);
  203.       }
  204.     bfd_put_8 (in_abfd, gap, data + *dst_ptr);
  205.     (*dst_ptr)++;
  206.     (*src_ptr)++;
  207.     break;
  208.       }
  209.     default:
  210.       abort ();
  211.     }
  212. }
  213.  
  214. #define coff_reloc16_extra_cases extra_case
  215.  
  216. #include "coffcode.h"
  217.  
  218.  
  219. #undef  coff_bfd_get_relocated_section_contents
  220. #undef coff_bfd_relax_section
  221. #define  coff_bfd_get_relocated_section_contents bfd_coff_reloc16_get_relocated_section_contents
  222. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  223.  
  224. bfd_target z8kcoff_vec =
  225. {
  226.   "coff-z8k",            /* name */
  227.   bfd_target_coff_flavour,
  228.   true,                /* data byte order is big */
  229.   true,                /* header byte order is big */
  230.  
  231.   (HAS_RELOC | EXEC_P |        /* object flags */
  232.    HAS_LINENO | HAS_DEBUG |
  233.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  234.  
  235.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  236.   '_',                /* leading symbol underscore */
  237.   '/',                /* ar_pad_char */
  238.   15,                /* ar_max_namelen */
  239.   1,                /* minimum section alignment */
  240.   _do_getb64, _do_getb_signed_64, _do_putb64,
  241.      _do_getb32, _do_getb_signed_32, _do_putb32,
  242.      _do_getb16, _do_getb_signed_16, _do_putb16,    /* data */
  243.   _do_getb64, _do_getb_signed_64, _do_putb64,
  244.      _do_getb32, _do_getb_signed_32, _do_putb32,
  245.      _do_getb16, _do_getb_signed_16, _do_putb16,    /* hdrs */
  246.  
  247.   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
  248.    bfd_generic_archive_p, _bfd_dummy_target},
  249.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
  250.    bfd_false},
  251.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  252.    _bfd_write_archive_contents, bfd_false},
  253.  
  254.   JUMP_TABLE (coff),
  255.   COFF_SWAP_TABLE,
  256. };
  257.